package com.csq.sso.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import com.csq.common.pojo.ReturnResult;
import com.csq.common.pojo.StatusCode;
import com.csq.manager.mapper.AccountMapper;
import com.csq.manager.mapper.EnterpriseMapper;
import com.csq.manager.mapper.ResumeMapper;
import com.csq.manager.pojo.Account;
import com.csq.manager.pojo.Enterprise;
import com.csq.manager.pojo.Resume;
import com.csq.sso.service.RegisterService;

@Service
public class RegisterServiceImpl implements RegisterService {

	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private ResumeMapper resumeMapper;
	
	

	@Autowired
	private EnterpriseMapper enterpriseMapper;

	@Override
	public ReturnResult accountRigster(Account account) {

		// 补全 注册时间
		account.setRegisterDate(new Date(System.currentTimeMillis()));
		account.setSex("男");

		// md5加密
		account.setUserpass(DigestUtils.md5DigestAsHex(account.getUserpass()
				.getBytes()));

		account.setState((short) 0);

		int insert = accountMapper.insertSelective(account);

		if (insert == 0) {
			return ReturnResult.build(StatusCode.REGFAILL, "服务器错误, 注册失败");
		}
		
		
		
		// 创建简历
		
		Resume resume = new Resume();
		resume.setUserId(account.getUserId());
		
		int insertSelective = resumeMapper.insertSelective(resume);
		
		if (insertSelective == 0) {
			return ReturnResult.build(StatusCode.REGFAILL, "服务器错误, 注册時创建简历失败");
		}
		

		return ReturnResult.ok();
	}

	@Override
	public ReturnResult enterpriseRigster(Enterprise enterprise) {
		// 补全 注册时间
		enterprise.setRegisterDate(new Date(System.currentTimeMillis()));
		enterprise.setState((short) 0);

		// md5加密
		enterprise.setPassword(DigestUtils.md5DigestAsHex(enterprise
				.getPassword().getBytes()));

		int insert = enterpriseMapper.insertSelective(enterprise);

		if (insert == 0) {
			return ReturnResult.build(StatusCode.REGFAILL, "服务器错误, 注册失败");
		}

		return ReturnResult.ok();
	}

}
